<?php
/**
 *
 *
 * @author mattevigo
 *
 * @project Sandro del Pistoia
 * @created 20/mar/2009
 */
ini_set("memory_limit","50M"); //aumenta temporaneamente la memoria dedicata a PHP
require_once '../config.php';
require_once SESSION_INCLUDE;
require_once PHOTO_INCLUDE;
require_once DB;
require_once PHOTO;
require_once USER;
require_once SESSION;
require_once GALLERY;

// inizio sessione
session_start();

$db = get_session_db($db_config);

if(!user_is_logged($db))
	header("Location:login.php");
// RECUPERO I PARAMETRI DA PASSARE ALLA FUNZIONE PREDEFINITA PER L'UPLOAD
$dir = TEMP_PHOTO_DIR;
$file = $_FILES['pic'];
$path = $_FILES['pic']['tmp_name'];
$filename = $_FILES['pic']['name'];
$file_path = $dir . "/" . $filename;
$gallery_id = $_POST['select_gallery'];

//echo "gallery_id: ".$gallery_id."<br />";

// Upload del file e controllo esito
if (move_uploaded_file($path, $dir ."/". $filename) && accept($file, $allowed_image_type))
{
	//ridimensiono il file nel formato di default
	if(resize($file_path))
	//	echo " resized";
	//rinomino il file (hash.estensione)
	$file_hash = hash_file(HASH_ALGO, PHOTO_DIR."TEMP.PNG");
	$new_filename = $file_hash.".png";
	rename(PHOTO_DIR."TEMP.PNG", PHOTO_DIR.$new_filename);

	//elimino la foto uploadata
	unlink($file_path);

	//creo il thumbnail
	thumbnail(PHOTO_DIR.$new_filename);
	rename(PHOTO_DIR."THUMB.PNG", PHOTO_DIR.$file_hash.".thumb.png");

	//aggiorno il database inserendo un record Photo
	$new_photo = new Photo($db);
	$new_photo->setFilename($new_filename);
	$new_photo->set('photo_date', time());
	$new_photo->setHash($file_hash);
	$new_photo->setTitle($_POST['photo_title']);
	$new_photo->setDescription($_POST['photo_description']);

	$size_array = getimagesize(PHOTO_DIR.$new_filename);
	$new_photo->setHeight($size_array[1]);
	$new_photo->setWidth($size_array[0]);

	$new_photo->set('gallery_id', $_POST['select_gallery']);

	$new_photo->store();

}
?>
